<?php

include_once 'model/UserToolbarModel.php';
include_once 'view/UserToolbarView.php';
include_once 'model/HeaderModel.php';
include_once 'view/HeaderView.php';

class BaseControler {
	protected $view, $userToolbarView, $headerView;
	
	public function __construct() {
		$this->headerView = '';
	}
	
	public function index($params = null) {
		$this->page(Router::getModel(), Router::getView(), 'render', $params);
	}
	
	protected function page($model, $view, $viewAction, $params = null) {
		//$model = Router::getModel(); // // pobierze Model		
		if($model)
			$model = new $model;
		//$view = Router::getView(); // pobierze Widok
		if($view)
			$this->view = new $view;
			
		$headerModel = new HeaderModel();
		$headerData = $headerModel->getData($params);
		if(isset($data->headerEntries))
			foreach($headerData->headerEntries as &$entry)
				$this->addHeaderEntry($entry);
		if($model){					
			$data = $model->getData($params); // pobiera dane z Modelu
			if(isset($data->headerEntries))
				foreach($data->headerEntries as &$entry)
					$this->addHeaderEntry($entry);
		}else{			
			$data=NULL;
		}			
		
		
		$this->header();
		
		if($this->view) {			
			echo '<body>';
			$this->view->setData($data); // przekazuje dane do Widoku, by ten m�g� je wy�wietli�
			if($this->view->getShowUserToolbar()) {				
				$this->userToolbarView = new UserToolbarView();
				$userToolbarModel = new UserToolBarModel();
				$_data = $userToolbarModel->getData($params);
				$this->userToolbarView->setData($_data);
				$this->userToolbarView->render();
			}	
			if($this->view->getShowHeader()) {				
				
				$this->headerView = new HeaderView();
				
				$this->headerView->setData($headerData);
				$this->headerView->render();		
			}			
			$this->view->$viewAction(); // wyświetli stronę
			echo '</body>';
		}
	}
	
	private function header() {
		echo '<head>'."\n";
		echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">'."\n";
		echo "<title>".Config::PAGE_TITLE."</title>\n";
		echo '<script type="text/javascript" src="content/scripts/logout.js"> </script>';
		echo "<link rel='icon' href='content/images/ico.png' type='image/x-icon'/>\n";
		echo '<link rel="stylesheet" href="content/styles/main.css" />'."\n";
		echo '<script src="'.URI::getUrl().'content/scripts/jquery-1.7.2.js" type="text/javascript"></script>';
		echo $this->headerView;
		echo '<link rel="stylesheet" href="content/styles/userToolbar.css" />'."\n";
		echo '</head>'."\n";
	}
	
	public function addHeaderEntry($entry) {
		$this->headerView .= $entry."\n";
	}
}

?>
